<?php
function returnImage ($text) {
	
    $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
    //echo $text;
    $pattern = "/<img[^>]+\>/i";
    preg_match($pattern, $text, $matches);
	if( sizeof($matches) == 0 )
	{ $text='http://vnexpress.net/Files/Subject/3B/A2/8C/4B/thuydienabc.jpg';}
	else
    {
		$text = $matches[0];	
	}
    return $text;
}

function scrapeImage($text) {
    
    $pattern = '/src=[\'"]?([^\'" >]+)[\'" >]/'; 
    
preg_match($pattern, $text, $link);
	if( sizeof($link) == 0 )
	{ 
		$link='http://vnexpress.net/Files/Subject/3B/A2/8C/4B/thuydienabc.jpg';
		return $link;
	}
	else
    {
		$link = $link[1];
		$link = urlencode($link);	
		$link = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($link));
	    return html_entity_decode($link,null,'UTF-8');
	}
}
?>
<?php
require_once('inc/simplepie.inc');
$feed = new SimplePie();

// feed source
$feed->set_feed_url('http://vnexpress.net/rss/gl/kinh-doanh.rss');
//set up cache
$feed->enable_cache(true);
$feed->set_cache_duration(3600);
$feed->set_cache_location('cache');
// start
$feed->init();
$feed->handle_content_type();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tin Hung Company</title>
</head>
<body>
	<div align="left">
    <ul style="width:155px; list-style:none;">
    <?php foreach($feed->get_items(0,8) as $item) : ?>
        <li  style="height:50px; background-color:#e6eef0;padding :5px; border: 1px solid #CCCCCC; margin-left:-35px;">        
	        <a style="text-decoration:none" href="<?php echo $item->get_permalink();?>">
                <div style="width:30px; float:left;display:inline-block;position:relative">
                    <?php
                        $feedDescription = $item->get_description();
                        $image = returnImage($feedDescription);	
                        $image = scrapeImage($image);
                        echo '<img height="25" width="25" src="'.$image.'"/>';
                    ?>
                 </div>
                 <div style="width:145px;float:left;display:inline;position:relative"   >
                    <font style="font-size:13px;font-family:Arial, Helvetica, sans-serif"><?php echo $item->get_title();?></font>
                 </div>
                 <br style="clear:both" />
            </a>      
        </li>
    <?php endforeach; ?>
    </ul>
    </div>
</body>
</html>
